package com.amazon.shop.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.amazon.shop.bean.entity.Evalform;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.mapper.EvalformMapper;
import com.amazon.shop.mapper.PriductMapper;
import com.amazon.shop.mapper.StoreMapper;
import com.amazon.shop.service.HomeService;
import com.amazon.shop.service.IRelUserStoreService;
import com.amazon.shop.service.IUserService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 创建时间：14:37 2021/1/28
 *
 * @author lzj
 * @since JDK 1.8.0_171
 * 类说明：
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    PriductMapper priductMapper;

    @Resource
    EvalformMapper evalformMapper;

    @Resource
    StoreMapper storeMapper;

    @Autowired
    private IUserService userServuce;

    @Autowired
    private IRelUserStoreService relUserService;

    @Override
    public Ret stsdata(String operatorUserid,Integer isManager) {
        Integer userid = Integer.valueOf(operatorUserid);

        JSONObject result = new JSONObject();
        JSONObject totalmap = new JSONObject();
        QueryWrapper queryWrapper = new QueryWrapper();
        Integer storetotal;
        Integer evaltotal = 0;
        Integer evaltotalyes = 0;
        Integer evaltotalno = 0;
        Integer emaiilyes = 0;
        Integer emaiilno = 0;
        Map<Integer,Integer> evalweeknum = new HashMap<>();
        for (int i = 1; i <= 7; i++){
            evalweeknum.put(i,0);
        }
        QueryWrapper<Evalform> queryWrapper2 = new QueryWrapper();
        List<Evalform> evalforms = new ArrayList<>();
        if(isManager!=null&&isManager==1){
            storetotal = relUserService.count();
            evaltotal = evalformMapper.selectCount(queryWrapper2);
            queryWrapper2.eq("satisfied",1);
            evaltotalyes = evalformMapper.selectCount(queryWrapper2);
            queryWrapper2.clear();
            queryWrapper2.eq("satisfied",0);
            evaltotalno = evalformMapper.selectCount(queryWrapper2);

            queryWrapper2.clear();
            queryWrapper2.eq("issend",1);
            emaiilyes = evalformMapper.selectCount(queryWrapper2);

            queryWrapper2.clear();
            queryWrapper2.eq("issend",0);
            emaiilno = evalformMapper.selectCount(queryWrapper2);

            Date startDate = DateUtil.beginOfWeek(DateUtil.beginOfDay(new Date()));
            Date endDate = DateUtil.endOfWeek(DateUtil.endOfDay(new Date()));
            queryWrapper2.clear();
            queryWrapper2.select("id","createtime");
            queryWrapper2.ge("createtime",startDate).lt("createtime",endDate);
            evalforms = evalformMapper.selectList(queryWrapper2);
        }else {
            queryWrapper.eq("userid",userid);
            storetotal = relUserService.count(queryWrapper);
            List<Integer> storeids = userServuce.listStoreidByUserid(operatorUserid);
            if(storeids!=null&&storeids.size()>0){
                queryWrapper2.in("storeid",storeids);
                evaltotal = evalformMapper.selectCount(queryWrapper2);

                queryWrapper2.eq("satisfied",1);
                evaltotalyes = evalformMapper.selectCount(queryWrapper2);

                queryWrapper2.clear();
                queryWrapper2.in("storeid",storeids);
                queryWrapper2.eq("satisfied",0);
                evaltotalno = evalformMapper.selectCount(queryWrapper2);

                queryWrapper2.clear();
                queryWrapper2.in("storeid",storeids);
                queryWrapper2.eq("issend",1);
                emaiilyes = evalformMapper.selectCount(queryWrapper2);

                queryWrapper2.clear();
                queryWrapper2.in("storeid",storeids);
                queryWrapper2.eq("issend",0);
                emaiilno = evalformMapper.selectCount(queryWrapper2);

                Date startDate = DateUtil.beginOfWeek(DateUtil.beginOfDay(new Date()));
                Date endDate = DateUtil.endOfWeek(DateUtil.endOfDay(new Date()));
                queryWrapper2.clear();
                queryWrapper2.select("id","createtime");
                queryWrapper2.in("storeid",storeids);
                queryWrapper2.ge("createtime",startDate).lt("createtime",endDate);
                evalforms = evalformMapper.selectList(queryWrapper2);
            }
        }
        totalmap.put("storetotal",storetotal);

        for (Evalform evalform : evalforms) {
            int i = DateUtil.dayOfWeek(evalform.getCreatetime());
            if(i == 1){
                i = 7;
            }else {
                i -= 1;
            }
            if (evalweeknum.get(i)!=0) {
                evalweeknum.put(i,evalweeknum.get(i)+1);
            }else {
                evalweeknum.put(i,1);
            }
        }
        totalmap.put("evaltotal",evaltotal);
        totalmap.put("evaltotalyes",evaltotalyes);
        totalmap.put("evaltotalno",evaltotalno);
        totalmap.put("emaiilyes",emaiilyes);
        totalmap.put("emaiilno",emaiilno);
        result.put("total",totalmap);
        result.put("evalweeknum",evalweeknum);
        return ResultUtil.success(result);
    }
}
